﻿Imports System.Data.OleDb

Public Class TaiKhoanDAO

    Public Function GetTable() As DataTable
        Dim dt As DataTable = New DataTable()
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim str As String = "select TenDangNhap, MatKhau, LoaiTK from TAIKHOAN"
        Dim da As OleDbDataAdapter = New OleDbDataAdapter(str, cn)
        da.Fill(dt)

        cn.Close()
        Return dt
    End Function

    Public Function Insert(ByVal tmp As TaiKhoanDTO) As Integer
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim str As String = "insert into TAIKHOAN (TenDangNhap, MatKhau, LoaiTK) values (@TenDangNhap, @MatKhau, @LoaiTK)"
        Dim cmd As OleDbCommand = New OleDbCommand(str, cn)

        Dim para1, para2, para3 As OleDbParameter
        para1 = New OleDbParameter("@TenDangNhap", tmp.TenDangNhap)
        cmd.Parameters.Add(para1)
        para2 = New OleDbParameter("@MatKhau", tmp.MatKhau)
        cmd.Parameters.Add(para2)
        para3 = New OleDbParameter("@LoaiTK", tmp.LoaiTK)
        cmd.Parameters.Add(para3)

        Dim result As Integer = cmd.ExecuteNonQuery()
        cn.Close()
        Return result
    End Function

    Public Function Update(ByVal tmp As TaiKhoanDTO) As Integer
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim str As String = "update TAIKHOAN set MatKhau=@MatKhau, LoaiTK=@LoaiTK where TenDangNhap=@TenDangNhap"
        Dim cmd As OleDbCommand = New OleDbCommand(str, cn)

        Dim para1, para2, para3 As OleDbParameter
        para1 = New OleDbParameter("@MatKhau", tmp.MatKhau)
        cmd.Parameters.Add(para1)
        para2 = New OleDbParameter("@LoaiTK", tmp.LoaiTK)
        cmd.Parameters.Add(para2)
        para3 = New OleDbParameter("@TenDangNhap", tmp.TenDangNhap)
        cmd.Parameters.Add(para3)

        Dim result As Integer = cmd.ExecuteNonQuery()
        cn.Close()
        Return result
    End Function

    Public Function Delete(ByVal tmp As TaiKhoanDTO) As Integer
        Dim cn As OleDbConnection = DataProvider.ConnectionData()
        Dim str As String = "delete from TAIKHOAN where TenDangNhap=@TenDangNhap"
        Dim cmd As OleDbCommand = New OleDbCommand(str, cn)

        Dim para1 As OleDbParameter
        para1 = New OleDbParameter("@TenDangNhap", tmp.TenDangNhap)
        cmd.Parameters.Add(para1)

        Dim result As Integer = cmd.ExecuteNonQuery()
        cn.Close()
        Return result
    End Function

End Class
